/*
 *
 * This is a VoIP Application designed for local-network usage for voice and
 * messaging
 * Copyright (C) 2012  Imran M Yousuf (imyousuf@smitsol.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.smitsol.voip.server.resources;

import com.smitsol.voip.api.domains.User;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author russel
 */
@Path("/users/{username}")
public class UserRegistrationResource extends AbstractResource {

  private static Logger logger = LoggerFactory.getLogger(UserRegistrationResource.class);
  private final String NAME = "name";
  private final String USERNAME = "username";
  private final String ID = "id";
  private final String PASSWORD = "password";
  private User user;
  private String username;

  public UserRegistrationResource(@PathParam("username") String username) {
    this.username = username;
  }

  @PostConstruct
  public void postConstruct() {
    user = getApi().getUserService().getUserByUsername(username);
  }

  @GET
  @Path("/content")
  @Produces(MediaType.APPLICATION_JSON)
  public Response getContent() {
    Response.ResponseBuilder responseBuilder = Response.status(Response.Status.OK);
    if (user == null) {
      return responseBuilder.status(Response.Status.NOT_FOUND).build();
    }
    responseBuilder.entity(user);
    return responseBuilder.build();
  }

  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  public Response update(Map<String, String> map) {
    Response.ResponseBuilder responseBuilder = Response.status(Response.Status.OK);
    if (user == null) {
      responseBuilder.status(Response.Status.NOT_FOUND).build();
    }
    if (map.get(ID) != null) {
      user.setId(map.get(ID));
    }
    if (map.get(NAME) != null) {
      user.setName(map.get(NAME));
    }
    if (map.get(USERNAME) != null) {
      user.setUsername(map.get(USERNAME));
    }
    if (map.get(PASSWORD) != null) {
      user.setPassword(map.get(PASSWORD));
    }


    try {
      getApi().getUserService().updateUser(user);
    }
    catch (Exception ex) {
      logger.error(ex.getMessage(), ex);
      responseBuilder.status(Response.Status.INTERNAL_SERVER_ERROR);
    }

    return responseBuilder.build();
  }

  @DELETE
  public Response delete() {
    Response.ResponseBuilder responseBuilder = Response.status(Response.Status.OK);
    if (user == null) {
      responseBuilder.status(Response.Status.NOT_FOUND).build();
    }
    try {
      getApi().getUserService().deleteUser(user);
    }
    catch (Exception ex) {
      responseBuilder.status(Response.Status.INTERNAL_SERVER_ERROR);
    }
    return responseBuilder.build();
  }
}
